package com.zl.learning.arithmetic.my;

import java.util.Arrays;
import java.util.Random;

/**
 * 先使头部有序，
 * 依次向后，每次循环找出最小值的下标
 * 循环结束后，交换数据
 */
public class SelectSort {
    private static int[] a = new Random().ints(0, 100).limit(7).toArray();

    /**
     * 每一趟内循环，选出最小的。然后交换
     */
    private static void selectSort(int[] a) {
        for (int i = 0; i < a.length - 1; i++) {
            int k = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[j] < a[k]) {
                    k = j;
                }
            }
            if (k != i) {
                int tmp = a[k];
                a[k] = a[i];
                a[i] = tmp;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(a));
        selectSort(a);
        System.out.println(Arrays.toString(a));
    }

}
